const app = getApp()
import prizeApi from '../../api/prizeApi'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    prizeList : [],
    isMore:true,
    type: '',
    keyword: ''
  },
  pageData: {
    pageNum : 0,
    pageSize : 8
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: async function (options) {
   
    wx.showLoading({
      title: '加载中',
    })
    
    await app.getOpenid()

    this.getAllPrize()

   
    wx.hideLoading()
  
  },

  onShow: function () {

  },

  

  search(e){

    this.data.keyword = e.detail.value

    this.pageData.pageNum = 0
    
    this.setData({
      prizeList: [],
      isMore: true
    })
      
    this.getAllPrize()
  },

  getAllPrize(){
  
    if(!this.data.isMore){
      return
    }
    this.pageData.pageNum ++

    var params = {
      pageNum : this.pageData.pageNum,
      pageSize : this.pageData.pageSize,
      openid: wx.getStorageSync('openid')
    }
    if(this.data.keyword){
      params.content = this.data.keyword
    }

    wx.showLoading({
      mask: true
    })
    prizeApi.getAllPrize(params).then(res => {
      wx.hideLoading()
      console.log(res)
      if(res.length < this.pageData.pageSize){
        this.setData({
          isMore:false
        })
      }
     
      var prizeList = this.data.prizeList.concat(res)
      this.setData({
        prizeList:prizeList
      })
    })
  },



  toDetail(e){
    var prize = e.currentTarget.dataset.prize
   
    wx.navigateTo({
      url: '/prize/prizeDetail/prizeDetail?prizeId='+prize._id,
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },


 

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.getAllPrize()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
   
  }
})